<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			*{
				padding: 0px;
				margin: 0px;
			}
		</style>
		<script type="text/javascript">
			window.onload=function(){
/*键盘事件练习：用上下左右键控制图片移动*/
			var img=document.getElementById("img");
			var img2=document.getElementById("img2");
			var dir=0;
			var speed=30;
			timer1=setInterval(function(){						
				switch(dir){
					case 37:
						img.style.left=img.offsetLeft-speed+"px";
						break;
					case 38:
						img.style.top=img.offsetTop-speed+"px";
						break;
					case 39:
						img.style.left=img.offsetLeft+speed+"px";
						break;
					case 40:
						img.style.top=img.offsetTop+speed+"px";
						break;
				}
				},50)
/*按住Ctrl键加速*/	
			document.onkeydown=function(event){
				event=event ||window.event;
				if(event.ctrlKey){/*按住Ctrl键改变速度*/
					speed=50;
				}else{
					speed=30;
				}
				dir=event.keyCode;
			}
			document.onkeyup=function(){
				dir=0;
			}
		}


			var imgx=[];
			var random,n;
			var index=1;
			var last=0;
/*敌方飞机移动函数*/			
			function diji(img,str){
				if(str="diji"){
					img.style.top=img.offsetTop+12+"px";					
					if(parseInt(img.style.top)>650){
						img.parentNode.removeChild(img);
					}
				}
				else{
					
				}
				
			}
/*敌方飞机创建函数(利用随机数在随机位置创建)*/
			function newdiji(img2){
					index=index+1;
					random=Math.random()*1300;
					last=random;
					while(random>last &&random<last+80 || random<last &&random>last-80){
						random=Math.random()*1300;
					}
					imgx[index]=document.createElement("img");
					imgx[index].src="img/e.png";
					imgx[index].style.position="absolute";
					imgx[index].style.left=random+"px";					
					imgx[index].id=index;	
					img2.parentNode.appendChild(imgx[index]);
					imgx[index]=document.getElementById(index);
					diji(imgx[index],"diji");
					/*console.log(imgx[index]);*/
					return imgx[index];
			}
/*我方发射的子弹*/
			var imgx1=[];
			var random1,n1;
			var index1=0;
			var f=false;
			function zidan(imgx1){
				imgx1.style.top=imgx1.offsetTop-20+"px";
/*删除子弹*/			/*if(parseInt(imgx1.style.top)<=0){
						imgx1.parentNode.removeChild(imgx1);
					}*/			
		
			}
			function newzidan(img){
					index1=index1+1;
					imgx1[index1]=document.createElement("img");
					imgx1[index1].src="img/8.jpg";
					imgx1[index1].style.position="absolute";
					imgx1[index1].style.left=img.offsetLeft+"px";
					imgx1[index1].style.top=img.offsetTop-37+"px";
					imgx1[index1].id=index1*1024;	
					img.parentNode.appendChild(imgx1[index1]);
					imgx1[index1]=document.getElementById(index1*1024);
					zidan(imgx1[index1]);
					return imgx1[index1];	
			}
/*设置定时器*/
			timer2=setInterval(function(){
				n=n+1;
				newdiji(img2);				
				imgx[n]=newdiji(img2);
/*循环所有敌机下落*/
				for(var i=2;i<index+1;i++){
					diji(imgx[i],"diji");
/*碰撞判定*/			
					if(img.offsetTop<imgx[i].offsetTop+imgx[i].offsetHeight && img.offsetTop>imgx[i].offsetTop-img.offsetHeight && img.offsetLeft<imgx[i].offsetLeft+imgx[i].offsetWidth && img.offsetLeft>imgx[i].offsetLeft-img.offsetWidth){
					img.src="img/w.png";
					setTimeout(function(){
							imgx[i].style.top=700+"px";
							alert("game over");
						},200)
					
					}
				}
/*创建子弹，循环发射*/
				n1=n1+1;
				imgx1[n1]=newzidan(img);
				
				/*console.log(imgx1);*/
				for(var j=1;j<index1+1;j++){
					zidan(imgx1[j],"zidan");
					for(var i=2;i<index1+1;i++){
						if(imgx1[j].offsetTop<imgx[i].offsetTop+imgx[i].offsetHeight && imgx1[j].offsetTop>imgx[i].offsetTop-img.offsetHeight && imgx1[j].offsetLeft<imgx[i].offsetLeft+imgx[i].offsetWidth && imgx1[j].offsetLeft>imgx[i].offsetLeft-imgx1[j].offsetWidth){
						imgx[i].src="img/w.png";
						
						setTimeout(function(){
							imgx[i].style.top=700+"px";
							/*imgx[i].style.opacity=0.0000001;*/
						},200)
						
						
						}
					}
				}
			},200)
		</script>
	</head>
	<body style="position: fixed;">
			<img src="img/9.jpg" width="1400px"height="700px"/>
			<img src="" style="position: absolute;" id="img2" />
			<img src="img/q.png" style="position: absolute;top: 470px;  left: 600px;z-index: 99999;" id="img"/>
			
	</body>
</html>
